<?php

namespace backend\widgets;

use backend\helpers\MatchHelper;
use backend\models\product\Log;
use Yii;
use yii\base\Widget;


class ProductLogList extends Widget
{
    public $product_id;

    public function init()
    {
        parent::init();

        if (empty($this->product_id)) {
            throw new InvalidConfigException('参数错误！');
        }
    }

    public function run()
    {
        $data = Log::find()
            ->where(['product_id'=>$this->product_id])
            ->orderBy('id desc')
            ->asArray()
            ->all();
        if (empty($data)) {
            return '<p>暂无记录。</p>';
        }

        $return = '<table class="table table-striped table-bordered">';
        $return .= "<tr><th>类型</th><th>数量</th><th>时间</th><th>操作人</th></tr>";
        foreach ($data as $row) {
            $type = $row['type'] == 2 ? '减少' : '增加';
            $operator = MatchHelper::getOperator($row['created_by']);
            $return .= "<tr><td>{$type}</td><td>{$row['qty']}</td><td>{$row['created']}</td><td>{$operator}</td></tr>";
        }
        $return .= "</table>";

        return $return;
    }
}